<?php

namespace App\Http\Controllers;

use App\Http\Requests\EditPermissionsRequest;
use App\MakeTable\Table;
use App\Permission;
use App\Redict\Success;
use Illuminate\Http\Request;


class PermissionController extends Controller
{
    public function index(){
        $pagename = '权限管理';
        $modelurl = 'permissions';
        $tablecate = ['tablecate'=>'权限'];
        $headname = ['权限字段名','权限标签','操作'];
        $tablehead = ['name','label','id'];
        $permissionstable = new Table($tablehead,$page = 8);
        $model = $permissionstable->viewdata(Permission::class);
        $models = $permissionstable->viewpagedata(Permission::class);
        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));
    }
    public function edit($id){
        $modelurl = 'permissions';
        $permission = Permission::findOrFail($id);

        $pagename = $permission->name;


        return view('admin.home.permissionedit',compact('pagename','permission','modelurl'));

    }
    public function update(EditPermissionsRequest $request,$id){

        $role = Permission::findOrFail($id);

        $role->update($request->all());

        return Success::back();
    }
    public function create(){
        $modelurl = 'permissions';
        $pagename = '添加权限';
        return view('admin.home.permissionform',compact('pagename','modelurl'));
    }
    public function store(EditPermissionsRequest $request){

        $role = Permission::create($request->all());

        return redirect('admin/permissions')->with('message', '成功添加'.$role->name);
    }
    public function destroy($id){
        Permission::destroy($id);
        return Success::back();
    }
}
